package servlet;

import util.GraphicHelper;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.io.OutputStream;

/**
 * @author ml
 * @create 2018-02-09--16:18
 */
@WebServlet("/regist.do")
public class VerifyCodeServlet extends HttpServlet {

    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        获得 当前请求的会话对象
        HttpSession session=req.getSession();

//        从请求中获得uri
        String uri=req.getRequestURI();
        System.out.println("uri = " + uri);

//        图片宽度
        final int width=180;

//        图片高度
        final int height=40;
//        图片格式
        final String imgType="jpeg";


//        得到输出流
        final OutputStream outputStream=resp.getOutputStream();
//        创建验证码并得到字符串
        String code= GraphicHelper.create(width,height,imgType,outputStream);

//        将字符串存入会话对象中
        session.setAttribute(uri,code);

        System.out.println(session.getAttribute(uri));
    }
}
